﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.CRM.Library;
using ZX.CRM.Bussiness;
using ZX.Web.UI.Controls.ControlBind;
using ZX.SYS.Library;
using ZX.BLL;
using ZX.DBModule;
using ZX.BLL.Extend;

namespace ZX.WebApp.Crm.Customer
{
    public partial class AddCrmCustomerChannel : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();

        protected void Page_Load(object sender, EventArgs e)
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            string str = CreateInstanceJsObject(instance, entity);
            this.EditTemplate.Text = str.Replace("@@@@", "");

            //省份
            IList<SysProvince> ListProvince = bll.GetAllProvince();
            DropDownListControl.DropDataSourceBind(this.Province, ListProvince, "Name", "Code");
            //(员工人数)
            IList<DatPhraseDetail> ListDetailNumber = bllDetail.GetDatPhraseDetailByClassValue("CustomerScope");
            DropDownListControl.DropDataSourceBind(this.PersonId, ListDetailNumber, "PhraseText", "PhraseID");
            //客户类型
            IList<DatPhraseDetail> ListDetailType = bllDetail.GetDatPhraseDetailByClassValue("CustomerType");
            DropDownListControl.DropDataSourceBind(this.TypeId, ListDetailType, "PhraseText", "PhraseID");
            //客户级别
            IList<DatPhraseDetail> ListDetailLevel = bllDetail.GetDatPhraseDetailByClassValue("CustomerLevel");
            DropDownListControl.DropDataSourceBind(this.LevelId, ListDetailLevel, "PhraseText", "PhraseID");
            //产值
            IList<DatPhraseDetail> ListDetailProduct = bllDetail.GetDatPhraseDetailByClassValue("Product");
            DropDownListControl.DropDataSourceBind(this.ProductId, ListDetailProduct, "PhraseText", "PhraseID");
            //客户来源
            IList<DatPhraseDetail> ListDetailOrigin = bllDetail.GetDatPhraseDetailByClassValue("CustomerSource");
            DropDownListControl.DropDataSourceBind(this.OriginId, ListDetailOrigin, "PhraseText", "PhraseID");
            //公司性质
            IList<DatPhraseDetail> ListDetailProperty = bllDetail.GetDatPhraseDetailByClassValue("Property");
            DropDownListControl.DropDataSourceBind(this.PropertyId, ListDetailProperty, "PhraseText", "PhraseID");

            if (DataAction == "insert")
            {
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            string strFlag = this.GetStringFromQueryString("flag", "");
            string strType = this.GetStringFromQueryString("type", "");
            if (strFlag == "0") //新增
            {
                this.Code.Value = IDMaker.GetSequenceStr("CQD001");
            }
            if (strFlag == "1" || strType == "1") //编辑
            {
                this.WorkFlowInsId.Value = this.GetStringFromQueryString("WorkflowInsId", "");
                this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                string workflowInsId = this.GetStringFromQueryString("WorkflowInsId", "");
                IList<V_CrmCustomerChannel> list = bll.GetAllV_CrmCustomerChannel(new Guid(workflowInsId));
                if (list != null && list.Count > 0)
                {
                    this.ID.Value = list[0].ID.ToString();
                    this.Code.Value = list[0].Code.ToString();
                    this.CustomerName.Value = list[0].CustomerName.ToString();

                    IList<V_SysProvince> ListProvinces = bll.GetProvinceInfo(list[0].Province);
                    if (ListProvinces != null && ListProvinces.Count > 0)
                    {
                        this.Province.SelectedIndex = this.Province.Items.IndexOf(this.Province.Items.FindByText(ListProvinces[0].Province));
                    }
                    IList<V_SysProvince> ListCity = bll.GetProvinceInfo(list[0].Province, list[0].City);
                    if (ListCity != null && ListCity.Count > 0)
                    {
                        //绑定城市
                        IList<SysCity> city = bll.GetAllCityByProviceName(list[0].Province);
                        DropDownListControl.DropDataSourceBind(this.City, city, "Name", "Code");
                        this.City.SelectedIndex = this.City.Items.IndexOf(this.City.Items.FindByText(ListCity[0].City));
                    }
                    IList<V_SysProvince> ListArea = bll.GetProvinceInfo(list[0].Province, list[0].City);
                    if (ListArea != null && ListArea.Count > 0)
                    {
                        //绑定区县
                        this.Area.DataSource = ListArea;
                        this.Area.DataTextField = "Area";
                        this.Area.DataValueField = "AreaCode";
                        this.Area.DataBind();

                        this.Area.SelectedIndex = this.Area.Items.IndexOf(this.Area.Items.FindByText(list[0].Area));
                    }
                    this.Address.Value = list[0].Address.ToString();
                    this.Decisionmaker.Value = list[0].Decisionmaker.ToString();
                    this.DecisionTel.Value = list[0].DecisionTel.ToString();
                    this.DecisionTel2.Value = list[0].DecisionTel2.ToString();
                    this.DecisionPosition.Value = list[0].DecisionPosition.ToString();
                    this.Tel1.Value = list[0].Tel1.ToString();
                    this.Tel2.Value = list[0].Tel2.ToString();
                    this.CompanyProduct.Value = list[0].CompanyProduct.ToString();
                    this.ManagerQuestion.Value = list[0].ManagerQuestion.ToString();
                    this.StrongPoint.Value = list[0].StrongPoint.ToString();
                    this.Explain.Value = list[0].Explain.ToString();
                    this.AddTime.Value = list[0].AddTime.ToString();
                    this.AreaId.Value = list[0].AreaId.ToString();
                    this.txtAreaId.Value = list[0].CustomerArea.ToString();
                    this.TradeId.Value = list[0].TradeId.ToString();
                    this.txtTradeId.Value = list[0].CustomerTrade.ToString();

                    DropDownListControl.DropDownListSelect(this.DecisionSex, list[0].DecisionSex.ToString());
                    DropDownListControl.DropDownListSelect(this.PersonId, list[0].PersonId.ToString());
                    DropDownListControl.DropDownListSelect(this.TypeId, list[0].TypeId.ToString());
                    DropDownListControl.DropDownListSelect(this.LevelId, list[0].LevelId.ToString());
                    DropDownListControl.DropDownListSelect(this.ProductId, list[0].ProductId.ToString());
                    DropDownListControl.DropDownListSelect(this.OriginId, list[0].OriginId.ToString());
                    DropDownListControl.DropDownListSelect(this.PropertyId, list[0].PropertyId.ToString());
                }
            }
        }

        /// <summary>
        /// 添加人ID
        /// </summary>
        public string UserID
        {
            get { return this.UserId.ToString(); }
        }
        /// <summary>
        /// 页面标题
        /// </summary>
        public string PageTitle
        {
            get { return this.GetStringFromQueryString("title", ""); }
        }
    }
}